package com.example.utils.util.UUID;

import com.github.f4b6a3.ulid.Ulid;
import com.github.f4b6a3.ulid.UlidCreator;
import com.github.f4b6a3.ulid.UlidFactory;

import java.time.Instant;
import java.util.Random;
import java.util.SplittableRandom;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Description     这是通用唯一词典可排序标识符的 Java 实现。
 *                      按生成时间排序;
 *                      可以存储为 UUID/全球通用报告;
 *                      可以存储为26个字符的字符串;
 *                      可以存储为16字节的数组;
 *                      字符串格式编码为克罗克福德的 base32;
 *                      字符串格式是 URL 安全的，不区分大小写，并且没有连字符。
 *                  ULID 是一个 128 位长的标识符。前 48 位表示自 Unix Epoch（1970-01-01）以来的毫秒数。剩余的 80 位由安全随机数生成器生成。其规范字符串表示形式的长度为 26 个字符。
 *                  https://github.com/f4b6a3/ulid-creator
 * @ClassName ulidutil
 * @Author jiang_11445266119198
 * @Date 2022/10/15 19:51
 */
public class ulidutil {
    public static void main(String[] args) {
        //创建一个乌里达：
        Ulid ulid = UlidCreator.getUlid();
        /*
         01EX8Y7M8MDVX3M3EQG69EEMJW
        01EX8Y7M8MDVX3M3EQG69EEMJX
        01EX8Y7M8MDVX3M3EQG69EEMJY
        01EX8Y7M8MDVX3M3EQG69EEMJZ
        01EX8Y7M8MDVX3M3EQG69EEMK0
        01EX8Y7M8MDVX3M3EQG69EEMK1
        01EX8Y7M8MDVX3M3EQG69EEMK2
        01EX8Y7M8MDVX3M3EQG69EEMK3
        01EX8Y7M8N1G30CYF2PJR23J2J < millisecond changed
        01EX8Y7M8N1G30CYF2PJR23J2K
        01EX8Y7M8N1G30CYF2PJR23J2M
        01EX8Y7M8N1G30CYF2PJR23J2N
        01EX8Y7M8N1G30CYF2PJR23J2P
        01EX8Y7M8N1G30CYF2PJR23J2Q
        01EX8Y7M8N1G30CYF2PJR23J2R
        01EX8Y7M8N1G30CYF2PJR23J2S
                 ^ look          ^ look

        |---------|--------------|
            time      random
         */
        //创建单调乌里迪： 单调性乌利德是乌利德的变体。每当当前毫秒等于前一毫秒时，随机分量就会递增 1。它的主要优点是速度。
        Ulid ulid1 = UlidCreator.getMonotonicUlid();

        //更多示例
        //从规范字符串（26 个字符）创建 ULID：
        Ulid ulid2 = Ulid.from("0123456789ABCDEFGHJKMNPQRS");

        //将 ULID 转换为小写的规范字符串：
        String string = ulid.toLowerCase(); // 0123456789abcdefghjkmnpqrs

        //将乌利达转换为自动优化：
        UUID uuid = ulid.toUuid(); // 0110c853-1d09-52d8-d73e-1194e95b5f19

        //将乌利达转换为 RFC-4122 UUID v4：
        UUID uuid3 = ulid.toRfc4122().toUuid(); // 0110c853-1d09-42d8-973e-1194e95b5f19
        //                                                       ^ UUID v4

        //立即创建 ULID：
        Instant instant = ulid.getInstant(); // 2007-02-16T02:13:14.633Z

        // static method
        Instant instant1 = Ulid.getInstant("0123456789ABCDEFGHJKMNPQRS"); // 2007-02-16T02:13:14.633Z
        //一个密钥生成器，如有必要，可以轻松进行替换：
        String s = UlidCreator.getUlid().toString();
        /*package com.example;
        import com.github.f4b6a3.ulid.UlidCreator;
                public class KeyGenerator {
                    public static String next() {
                        return UlidCreator.getUlid().toString();
                    }
                }
        String key = KeyGenerator.next();
         */

        //A 与 ：UlidFactoryjava.util.Random
        // use a `java.util.Random` instance for fast generation
        UlidFactory factory = UlidFactory.newInstance(new Random());
        // use the factory
        Ulid ulid4 = factory.create();
        //A 与 ：UlidFactorySplittableRandom

        // use a random function that returns a long value
        SplittableRandom random = new SplittableRandom();
        UlidFactory factory1 = UlidFactory.newInstance(() -> random.nextLong());
        // use the factory
        Ulid ulid5 = factory1.create();

        //A 与 （JDK 17+）：UlidFactoryRandomGenerator
        // use a random function that returns a long value
        //RandomGenerator random = RandomGenerator.getDefault();
        //UlidFactory factory = UlidFactory.newInstance(() -> random.nextLong());
        // use the factory
        //Ulid ulid6 = factory.create();

        //A 与 ：UlidFactoryThreadLocalRandom
        // use a random supplier that returns an array of 10 bytes
        UlidFactory factory2 = UlidFactory.newInstance((length) -> {
            final byte[] bytes = new byte[length];
            ThreadLocalRandom.current().nextBytes(bytes);
            return bytes;
        });
        // use the factory
        Ulid ulid7 = factory2.create();



    }
}
